package gokyesil.game.aygitciklar.screens;

import java.util.ArrayList;
import java.util.LinkedList;

import gokyesil.game.aygitciklar.controller.AygitController;
import gokyesil.game.aygitciklar.model.Aygit;
import gokyesil.game.aygitciklar.model.Box;
import gokyesil.game.aygitciklar.model.Earth;
import gokyesil.game.aygitciklar.model.EndBox;
import gokyesil.game.aygitciklar.model.WhiteBox;
import gokyesil.game.aygitciklar.view.Renderer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.ModelLoader;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.lights.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.lights.Lights;
import com.badlogic.gdx.graphics.g3d.loader.ObjLoader;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.Screen;






public class GameScreen implements Screen
{
	private ShapeRenderer shapeRenderer;
	private Renderer renderer;
	private OrthographicCamera camera;
	private Aygit aygit;
	private World world;
	private AygitController controller;
	private float timeStep;
	private int velocityIterations;
	private int positionIterations;
	public boolean reset;
	private static final float CAMERA_WIDTH = 25.0f;
	private static final float CAMERA_HEIGHT = 15.0f;
	private static final float ASPECT_RATIO = 1.66667f;
	
	
    public PerspectiveCamera cam;
    public CameraInputController camController;
    public ModelBatch modelBatch;
    private Model cube;
    public AssetManager assets;
    public Array<ModelInstance> instances = new Array<ModelInstance>();
    public Lights lights;
    public boolean loading;
	private float z;
	private float y;
	private float x;
	private float angle;
	private ModelInstance ins;
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		shapeRenderer = null;
		renderer = null;
		camera = null;
		aygit = null;
		world = null;
		controller = null;
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render(float delta) 
	{
		if(controller.reset)
		{
			reset = true;
		}
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	    world.step(delta, velocityIterations, positionIterations);
		controller.update();
		camera.update();
		cam.update();
		shapeRenderer.setProjectionMatrix(camera.combined);
		shapeRenderer.begin(ShapeType.Filled);
		shapeRenderer.rect(70, 50, 5, 5);
		shapeRenderer.end();
		renderer.render(camera,controller.canBeFinished, controller.isFinished, cam, modelBatch, ins, lights);
	}

	public void resize(int width, int height) 
	{		
		System.out.println("width: "+width+"   height: "+height);
		float ratio = (float)width / (float)height;
		Vector2 trans = new Vector2();
		if(ratio < ASPECT_RATIO)
		{
			height = (int)(CAMERA_HEIGHT*ASPECT_RATIO/ratio);
			width =  (int) CAMERA_WIDTH;
			trans.set(0,height-CAMERA_HEIGHT);
		}
		else
		{
			width = (int)(CAMERA_WIDTH*ratio/ASPECT_RATIO);
			height =  (int) CAMERA_HEIGHT;
			trans.set(width-CAMERA_WIDTH,0);
		}
		camera.setToOrtho(false, width, height);
		camera.position.set((width- trans.x)/2.0f, (height-trans.y)/2.0f,0);
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	public void show() 
	{
		reset =false;
		timeStep = 1f / 60.f;
	    velocityIterations = 6;
	    positionIterations = 2;
		Vector2 gravity = new Vector2(0.0f,-10.0f);
		world = new World(gravity, true);
		aygit = new Aygit(world, 4, 15);
		EndBox eBox = new EndBox(world, 22.4f, 4.8f);
		LinkedList<Box> AIList = new LinkedList<Box>();
		AIList.add(new WhiteBox(world, 16.0f, 6.4f));
		controller = new AygitController(aygit.body, eBox.body, AIList.getFirst().body, world);
		Gdx.input.setInputProcessor(controller);
		Gdx.gl.glClearColor(0.0f, 0.0f, 254.0f, 0);
		ArrayList<Earth> groundList = new ArrayList<Earth>();
		groundList.add(new Earth(world, 0.0f, 0.0f, 1, 10));
		groundList.add(new Earth(world, 1.6f, 8.0f, 2, 1));
		groundList.add(new Earth(world, 6.4f, 4.8f, 1, 7));
		groundList.add(new Earth(world, 1.6f, 0.0f, 11, 1));
		groundList.add(new Earth(world, 3.2f, 4.8f, 2, 1));
		groundList.add(new Earth(world, 11.2f, 1.6f, 5, 1));
		groundList.add(new Earth(world, 12.8f, 3.2f, 4, 1));
		groundList.add(new Earth(world, 22.4f, 0.0f, 1, 3));
		//groundList.add(new Earth(world, 11.0f, 3.0f, 5, 1));
		renderer = new Renderer(aygit,groundList,AIList,eBox);
		camera = new OrthographicCamera();   
		shapeRenderer = new ShapeRenderer();
		shapeRenderer.setColor(new Color(100,50,40,30));
		//camera.setToOrtho(false, 960, 600);
		//camera.position.set(960 / 2, 600 / 2, 0);
		
		
		modelBatch = new ModelBatch();
        lights = new Lights();
        lights.ambientLight.set(40f, 40f, 40f, 100f);
        lights.add(new DirectionalLight().set(10f, 10f, 10f, 0f, 0f, 0f)); 
        cam = new PerspectiveCamera(30, CAMERA_WIDTH, CAMERA_HEIGHT);
        cam.position.set(0f, 0f, 28.1f);
        cam.lookAt(0,0,0);
        cam.near = 0.1f;
        cam.far = 300f;
        cam.update();
        
        ModelLoader loader = new ObjLoader();
        cube = loader.loadModel(Gdx.files.internal("assets/cube.obj"));    
        ins = new ModelInstance(cube,0,0,0);
	}

}
